from django.db import models
from django import forms
from django.contrib import admin
from .models import ChatSession, ChatHistory, Mask

# Register your models here.


class ChatHistoryAdmin(admin.ModelAdmin):
    search_fields = ['chatID__chatID']
    list_display = ('id', 'chatID', "get_chat_id", "title", "chat_session_state", "send_token", "receive_token")
    readonly_fields = ('message',)
    list_per_page = 20

    def get_chat_id(self, obj: ChatHistory):
        return obj.chatID.chatID
    get_chat_id.short_description = '会话ID'

    def chat_session_state(self, obj: ChatHistory):
        return obj.chatID.state
    chat_session_state.boolean = True
    chat_session_state.short_description = '用户是否已经删除会话'


class ChatSessionAdmin(admin.ModelAdmin):
    search_fields = ['userID__userID', 'chatID']
    list_display = ('userID', 'chatID', "state", "get_title")
    list_per_page = 20

    def get_title(self, obj: ChatSession):
        query = ChatHistory.objects.get(chatID=obj.chatID)
        return query.title
    get_title.short_description = '标题'


class MaskAdmin(admin.ModelAdmin):
    search_fields = ['title', 'desc']
    list_display = ('maskId', 'title', "count_desc", "model")
    list_per_page = 20

    def count_desc(self, obj: Mask):
        return obj.desc.count('|') + 1
    count_desc.short_description = '预设对话数'


admin.site.register(ChatSession, ChatSessionAdmin)
admin.site.register(ChatHistory, ChatHistoryAdmin)
admin.site.register(Mask, MaskAdmin)
